package tgc.edu.fhh.web.controller;


import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import tgc.edu.fhh.custom.AjaxResult;
import tgc.edu.fhh.custom.FileUtils;
import tgc.edu.fhh.custom.TablePageable;
import tgc.edu.fhh.entity.Student;
import tgc.edu.fhh.entity.TbClass;
import tgc.edu.fhh.service.StudentService;
import tgc.edu.fhh.web.form.StudentForm;

@RestController
@RequestMapping(value = "/student")
public class StudentController {
	@Autowired
	private StudentService service;
	@Autowired
	private FileUtils fileUtil;
	
	
	@RequestMapping(value = "/page")
	public Object page(TablePageable page,StudentForm form) {
		PageRequest pagerequest = page.buildPageRequest();
		Specification<Student> spec = buildSpec(form);
		Page<Student> page2 = service.findAll(spec,pagerequest);
		return new AjaxResult(AjaxResult.buildTableResult(page2));
	}
	
	@RequestMapping(value = "/search")
	public void search(StudentForm form,ModelMap modelmap) {
		modelmap.put("model", form);
	}
	
	@RequestMapping(value = {"/save"})
	public Object save(StudentForm form) {
		Boolean flag = service.save(form);
		if(flag) {
			return new AjaxResult("数据保存成功");
		}
		return new AjaxResult(false, "数据保存失败");
	}
	@RequestMapping(value = {"/delete"})
	public Object del(Integer id,ModelMap modelmap) {
		Student student = service.findById(id);
		TbClass tbclass = student.getTbclass();
		Integer bjId = tbclass.getId();
		service.deleteById(id);
		modelmap.put("bjId", bjId);
		return bjId;
	}
	
	@RequestMapping(value = "/pwdup")
	@ResponseBody
	public Object pwdup(Integer id,ModelMap modelmap) {
		Student student = service.findById(id);
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		String password = encoder.encode("1234");
		student.setPassword(password);
		service.save(student);
		Integer bjId = student.getTbclass().getId();
		modelmap.put("bjId", bjId);
		return bjId;
	}
	
	@RequestMapping(value = "/download")
	public void download(StudentForm form,HttpServletRequest request,HttpServletResponse response) {
		Integer id = form.getId();
		Student student = service.findById(id);
		String filename = student.getFilename();
		String uuid = student.getUuid();
		fileUtil.download(request, response, uuid, filename);
	}

	private Specification<Student> buildSpec(StudentForm form) {
		Specification<Student> spec = new Specification<Student>() {
			
			@Override
			public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String search = form.getSearch();
				if (StringUtils.hasText(search)) {
					HashSet<Predicate> rules2 = new HashSet<>();
					rules2.add(cb.like(root.get("stuNo"),"%"+search+"%" ));
					rules2.add(cb.like(root.get("username"),"%"+search+"%" ));
					rules2.add(cb.like(root.get("name"),"%"+search+"%" ));
					rules2.add(cb.like(root.get("sex"),"%"+search+"%" ));
					rules.add(cb.or(rules2.toArray(new Predicate[rules2.size()])));
				}
				Integer tbclassId = form.getTbclassId();
				if (tbclassId!=null) {
					rules.add(cb.like(root.get("tbclass").get("id").as(String.class), "%"+tbclassId+"%"));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
}
